<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Style {

	public function start() {
		global $cache,$request;

		$action = $request->get( 'p' );

		switch( $action ) {
	    case 'change':
	        $this->change();
	    	break;
	    default:
	    	$this->show( $action );
		}
	}

	function change() {
		global $db,$cache,$request;
		$db->query( "UPDATE `" . DB_PREFIX . "set` SET style='" . $request->get( 'page' ) . "'" );
		$content = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'set' );
		$cache->set( 'config', $content[0], 0 );
		$cache->refresh( 'style', TRUE );
		echo '<p class="warning">应用新风格成功</p>';
	}

	private function show( $action ) {
		global $cache;
		@header("content-type: text/html; charset=utf-8");
?>
<div class = "admin_panel">
	<div class = "admin_title"><?php echo L('_THEME_MANAGEMENT_'); ?></div>
	<div class = "admin_content">
	<?php
		switch( $action ) {
		    default:
		        $this->main();
		}
	?>
	</div>
	<script>
		function ajax_get_style( BlogPath, M, P, E, I ){
			$("#admin_loading").css('visibility','visible');
			var URL = BlogPath + 'index.php?m=' + M;
			if( P != null ) {
				URL += '&p=' + P;
			}
			if( E != null ) {
				URL += '&page=' + E;
			}
			if( I != null ) {
				URL += '&id=' + I;
			}
			URL += '&ohash=' + ohash;
			$.get(URL,function(data){
				if(data=='forbidden'){
					document.location.href='./';
					return;
				}
				showMessage(data);
				if( data=='<p class="warning">应用新风格成功</p>' ){
					setTimeout("ajax_load('<?php echo ADMIN_PATH ?>','style');",200);
				}
				$("#admin_loading").css('visibility','hidden');
			});
		}
	</script>
</div>
<?
	}

	function main() {
		global $cache;
		include( APP_CORE . 'theme.class.php' );
?>
<div align="left">
	<?php
	$cur_theme = $cache->config['style'];
	$result = read_dir( APP_THEME );
	foreach ($result as $each_dir) {
		$each_dir = str_replace( APP_THEME, '', $each_dir );
		if( !Theme::is_legal( $each_dir ) ) continue;
		$styles[$each_dir]['directory'] = $each_dir;
		$styles[$each_dir]['path'] = THEME_PATH . $each_dir . '/';
		$infofile = APP_THEME . $each_dir . '/info.xml';
		$info = Theme::parse_xml_info( $infofile,'style' );
		$styles[$each_dir] = array_merge( $styles[$each_dir], $info );
	}
	foreach ($styles as $theme) {
	?>
	<dl class="theme<?php if ($theme['directory'] == $cur_theme) echo ' current';?>">
		<dt><a href="#" onclick="ajax_get_style('<?php echo ADMIN_PATH ?>','style','change','<?php echo $theme['directory']?>');return false;" title="应用该主题"><img src="<?php echo $theme['path'] . $theme['thumb']; ?>" /></a></dt>
		<dd class="theme_name"><?=$theme['name']?></dd>
		<dd>作者：<?php echo $theme['author']?></dd>
		<dd>版本：<?php echo $theme['version']?></dd>
		<dd>时间：<?php echo $theme['pubdate']?></dd>
		<dd>主页：<a href="<?php echo $theme['homepage']?>" target="_blank"><?php echo $theme['homepage']?></a></dd>
		<dd title="<?php echo $theme['intro']?>">简介：<?php echo cut_str($theme['intro'],60)?></dd>
	</dl>
	<?php }?>
	<div class="fixed"></div>
</div>
<?php
	}
}
/*
//=================================
// 代码编辑器
//=================================
Sub CodeEditor
	dim source
	source = Temp_Load( Request.QueryString("file") )
    source = Replace(source, ">", "&gt;")
    source = Replace(source, "<", "&lt;")
	%>
	<form action="admin_style.asp?action=EditTemplate&name=<%= Request.QueryString("file") %>" method="post">
		<div style="height:420px;">
			<div style="float:left;border:1px solid #808080;width:15%;height:385px;margin:10px;">
				<div style="height:180px;padding:2px;text-align:left;">
					<b>模板列表：</b>
					<br /><a href="admin_style.asp?action=CodeEditor&file=main.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />main.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=Head.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />Head.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=Foot.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />Foot.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=side.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />side.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=Widget.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />Widget.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=article.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />article.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=comment.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />comment.html</a>
					<br /><a href="admin_style.asp?action=CodeEditor&file=Message.html"><img border="0" src="../include/htm.gif" style="margin:4px 3px -3px 0px" />Message.html</a>
				</div>
				<div style="height:205px;padding:2px;text-align:left;">
					<b>帮助与提示：</b>
				</div>
			</div>
			<div style="width:75%;height:365px;border:1px solid #808080;margin:10px;padding:10px;float:left">
				<b>当前模板：<%= Request.QueryString("file") %></b><br /><br />
				<textarea name="myCode" wrap="on" style="width:100%;height:336px;"><%=source%></textarea>
			</div>
		</div>
		<input type="submit" name="Submit" class="main_button" value="保存" />
	</form>
	<div class="main_button"></div>
	<%
End Sub
*/
?>
